package com.zc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.zc.dao.ArticleDao;
import com.zc.model.Article;
import com.zc.model.User;
import com.zc.util.HibernateUtil;

public class ArticleDaoImpl implements ArticleDao {
	private DataSource dataSource;

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	// 向数据表article中添加数据
	@Override
	public boolean addArticle(Article article) {
		boolean flag = false;
		Session session = null;

		// Article article = new Article();
		// article.setTitle("2");
		// article.setContex("3");
		// article.setCreator("4");
		// article.setCrateTime("5");
		// article.setLastestEditor("6");
		// article.setLastestEditTime("7");

		// Hibernate的持久化操作
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(article);
			session.getTransaction().commit();
			flag = true;
		} catch (HibernateException e) {
			session.getTransaction().rollback();// 事务回滚
			System.out.println("数据添加失败");
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();// 关闭Session对象
		}
		return flag;

	}

	@Override
	public List<Article> getAllArticle() {
		List<Article> articleList = new ArrayList<Article>();
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			String hql = "from Article article";
			Query query = session.createQuery(hql);
			articleList = query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtil.closeSession();
		}
		return articleList;
	}

	@Override
	public Article getArticle(Article article) {

		Article tempArticle = null;
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			String hql = "from Article article where article.id=:id";
			Query q = session.createQuery(hql);
			q.setParameter("id", article.getId());
			List<Article> articleList = q.list();
			if (articleList != null && articleList.size() >= 1) {
				tempArticle = articleList.get(0);
				System.out.println(tempArticle.toString());
			}
			session.getTransaction().commit();
			// System.out.println(article.toString());
		} catch (HibernateException e) {
			System.out.println("对象装载失败");
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {

			HibernateUtil.closeSession();
		}
		return tempArticle;
	}

	@Override
	public boolean updateArticle(Article article) {
		boolean flag = false;
		Session session = null;

		// Article article = new Article();
		// article.setTitle("2");
		// article.setContex("3");
		// article.setCreator("4");
		// article.setCrateTime("5");
		// article.setLastestEditor("6");
		// article.setLastestEditTime("7");

		// Hibernate的持久化操作
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(article);
			session.getTransaction().commit();
			flag = true;
		} catch (HibernateException e) {
			session.getTransaction().rollback();// 事务回滚
			System.out.println("数据更新失败");
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();// 关闭Session对象
		}

		// TODO Auto-generated method stub
		return flag;
	}

	@Override
	public boolean deleteArticle(Article article) {
		boolean flag = false;
		Session session = null;
		// Hibernate的持久化操作
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(article);
			session.getTransaction().commit();
			flag = true;
		} catch (HibernateException e) {
			session.getTransaction().rollback();// 事务回滚
			System.out.println("数据删除失败");
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();// 关闭Session对象
		}

		return flag;
	}

}
